package com.fjh.controller;

import com.fjh.dto.OrderConfirmDto;
import com.fjh.service.OrderService;
import com.fjh.model.OrderStatus;
import com.fjh.model.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("p/myOrder")
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("orderCount")
    @ApiOperation("查询用户的订单状态数量")
    public ResponseEntity<OrderStatus> orderCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderStatus orderStatus = orderService.getUserOrderCount(userId);
        return ResponseEntity.ok(orderStatus);
    }

    @PostMapping("confirm")
    @ApiOperation("订单的确认")
    public ResponseEntity<OrderVo> orderConfim(@RequestBody OrderConfirmDto orderConfirmDto){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        OrderVo orderVo = orderService.orderConfim(userId, orderConfirmDto);
        return ResponseEntity.ok(orderVo);
    }

    @PostMapping("submit")
    @ApiOperation("下预订单")
    public ResponseEntity<String> orderSubmit(@RequestBody OrderVo orderVo){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        String orderSn = orderService.orderSubmit(userId,orderVo);
        return ResponseEntity.ok("orderSn:"+orderSn);
    }

}
